/*
 * 文件名：ArticleServiceImpl.java	 
 * 时     间：上午11:29:50
 * 作     者：liyf      
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.qianhai.news.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.entity.LoginUserInfo;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.DateUtil;
import com.qianhai.common.utils.RegExpUtil;
import com.qianhai.common.utils.ShiroUtil;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;
import com.qianhai.news.dao.mapers.ArticleMapper;
import com.qianhai.news.model.Article;
import com.qianhai.news.model.ArticleExample;
import com.qianhai.news.service.face.ArticleService;

/**
 * @ClassName: ArticleServiceImpl
 * @Description: 这里用一句话描述这个类的作用
 * @author liyf
 * @date 2014年11月14日 上午11:29:50
 */
@Service
public class ArticleServiceImpl extends
		BaseServiceImpl<ArticleExample, Article, String> implements
		ArticleService
{
	
	@Autowired
	private ArticleMapper articleMap;
	
	@Override
	public BaseMapper<ArticleExample, Article, String> getMapper()
	{
		
		return articleMap;
	}
	
	@Override
	public int addArticle(Article article) throws BaseException
	{
		try
		{
			LoginUserInfo user = ShiroUtil.getCurrentUser();
			
			if (null == article.getArticleTitle()
					|| "".equalsIgnoreCase(article.getArticleTitle()))
			{
				// 50000-00000-0002
				throw new BaseException("50000-00000-0002");
			}
			
			if(Tools.isEmpty(article.getArticleContent())){
				throw new BaseException("50000-00000-0005");
			}
			
			if (null == article.getArticletypeUuid()
					|| "".equalsIgnoreCase(article.getArticletypeUuid()))
			{
				// 50000-00000-0014
				throw new BaseException("50000-00000-0014");
			}  else if (null ==user)
			{
				throw new BaseException("10000-00003");
			}
			
			ArticleExample ex = new ArticleExample();
			ex.or().andArticleTitleEqualTo(article.getArticleTitle());
			List<Article> list  = articleMap.selectByCondition(ex);
			if(list !=null && !list.isEmpty()){
				throw new BaseException("50000-00000-0003");
			}
			if(Tools.isEmpty(article.getArticleCode())){
				article.setArticleCode(UUIDGenerateUtil.generate());
			}
			article.setArticleUuid(UUIDGenerateUtil.generate());
			article.setArticleCreatetime(DateUtil.getNowTimeStr());
			
			
			int num = articleMap.insertSelective(article);
			/**
			 * 文章标签的添加 暂时不用 if(article.getListLable()!=null &&
			 * article.getListLable().size()>0){ saveOrUpdateLable(article); }
			 */
			return num;
		} catch (Throwable e)
		{
			log.error(e.getMessage(), e);
			throw new BaseException(e.getMessage());
		}
		
	}
	
	@Override
	public int modifyArticle(Article article) throws BaseException
	{
		
		try
		{
			
			if (null == article.getArticleTitle()
					|| "".equalsIgnoreCase(article.getArticleTitle()))
			{
				// 50000-00000-0002
				throw new BaseException("50000-00000-0002");
			}
			
			if(Tools.isEmpty(article.getArticleUuid())){
				throw new BaseException("50000-00000-0004");
			}
			if(Tools.isEmpty(article.getArticleContent())){
				throw new BaseException("50000-00000-0005");
			}
			ArticleExample ex = new ArticleExample();
			ex.or().andArticleTitleEqualTo(article.getArticleTitle()).andArticleUuidNotEqualTo(article.getArticleUuid());
			List<Article> list  = articleMap.selectByCondition(ex);
			if(list !=null && !list.isEmpty()){
				throw new BaseException("50000-00000-0003");
			}
			
			int num = articleMap.updateByPrimaryKeySelective(article);
			
		
			return num;
		} catch (Throwable e)
		{
			log.error(e.getMessage(), e);
			throw new BaseException(e.getMessage());
		}
	}
	
	
}
